<?php

namespace Bete\Database;

use Bete\Foundation\Application;

class ConnectionFactory
{
    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function make(array $config, $name = null)
    {
        // $config = $this->parseConfig($config, $name);

        if (isset($config['read'])) {
            return $this->createReadWriteConnection($config);
        }

        return $this->createSingleConnection($config);
    }

    protected function createSingleConnection(array $config)
    {
        $connector = $this->createConnector($config);

        return $this->createConnection($connector, $config); 
    }

    protected function createConnector(array $config)
    {
        return function() use ($config) {
            return (new Connector($config))->connect();
        };
    }

    protected function createConnection($connector, array $config = [])
    {
        return new Connection($connector, $config);
    }

}